package com.wk.controller.student;

import com.wk.pojo.*;
import com.wk.pojo.Class;
import com.wk.service.Courseservice;
import com.wk.service.Userinfoservice;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @program: thesis
 * @description: 用于实现学生登录和注册
 * @author: 王侃
 * @create: 2020-02-13 15:33
 **/
@Controller
public class Stulogincontroller {

    @Autowired
    Userinfoservice userinfoservice;
    @Autowired
    Courseservice courseservice;

    //跳转到主界面
    @RequestMapping("/")
    public String main()
    {
        return "redirect:/community/index";
    }

    //跳转到登录界面
    @RequestMapping("/login")
    public String login()
    {
        return "student/login";
    }

    //跳转到注册界面
    @RequestMapping("/register")
    public String register()
    {
        return "student/register";
    }

    //登录提交
    @RequestMapping("/loginsubmit")
    public String loginsubmit(String username, String password,String role, Model model)
    {
        //获取当前用户subject
        Subject subject= SecurityUtils.getSubject();
        //进行Md5加密，第一个参数为密码，第二参数是用户名，第三个参数是加密的次数
        Md5Hash md5registepassword=new Md5Hash( password, username,5);
        //封装用户的登录数据
        UsernamePasswordToken token=new UsernamePasswordToken(username,md5registepassword.toString());

        try {
            subject.login(token);
            if (role.equals("stu"))
            return "redirect:/";
            else
                if (role.equals("tea"))
            return "redirect:/tea/main";
        }catch (UnknownAccountException e)//用户名不存在
        {
            model.addAttribute("msg","用户名不存在");
            return "student/login";
        }catch (IncorrectCredentialsException e)
        {
            model.addAttribute("msg","密码错误");
            return "student/login";
        }

        return "redirect:/";
    }

    //返回班级的类型
    @ModelAttribute("classtypes")
    public List<Class> classes()
    {
        List<Class> classes=userinfoservice.selectbyallclass();
        return classes;
    }

    //返回课程的类型
    @ModelAttribute("coursetypes")
    public List<Coursetype> coursetypes()
    {
        List<Coursetype> coursetypes=courseservice.selectbyalltype();
        return coursetypes;
    }

    //返回前8个课程
    @ModelAttribute("topcourses")
    public List<Course_topic> coursetopic()
    {
        List<Course_topic> topcourses=courseservice.selectbyalltopic();
        return topcourses;
    }


    //跳转到登录界面
    @RequestMapping("/student/success")
    public String success()
    {
        return "student/success";
    }

    //跳转到测试界面
    @RequestMapping("/test")
    public String test()
    {
        return "student/test";
    }

    //显示图片
    @GetMapping("/show/{user_id}")
    public void showmedia(@PathVariable("user_id") Integer user_id, OutputStream outputStream)
    {
        //通过user_id获得路径
       String path=userinfoservice.getimages(user_id);
       //显示即可
       userinfoservice.showmedia(path,outputStream);
    }


    //学生退出登录
    @RequestMapping("/stu/logout")
    public String logout(HttpSession session)
    {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "student/main";
    }

    //首页观看视频
    @RequestMapping("/first_video")
    public String first_video()
    {
        return "common/first_video";
    }

    //操作成功
    @RequestMapping("/common_success")
    public String common_success()
    {
        return "common/success";
    }

    //认证但是没有授权的访问此路径
    @RequestMapping("/unauth")
    @ResponseBody
    public String unauthorized()
    {
        return "未经授权无法访问";
    }

    //聊天室直接访问
    @RequestMapping("/chat")
    public String chat()
    {
        return "student/chat/login";
    }

    //聊天室开始
    @RequestMapping("/chat_submit")
    public String chat_submit(String name,Model model)
    {
        model.addAttribute("name",name);
        return "student/chat/chat_all_test";
    }
}
